package com.study.controller;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.study.model.pojo.Brand;
import com.study.model.pojo.Kind;
import com.study.model.pojo.Product;
import com.study.services.BrandService;
import com.study.services.KindService;
import com.study.services.ProductService;
import com.study.services.ProductServices;

@RestController
@CrossOrigin
public class ProductController {
	@Autowired
	ProductService pService;
	@Autowired
	BrandService bService;
	@Autowired
	KindService kService;
	@Autowired
	ProductServices ps;
	
	/**
	 * 查询产品信息
	 * @return
	 */
	@RequestMapping("/findproduct")
	public JSONObject findwarehouse() {
		JSONObject json=new JSONObject();
		JSONArray json1=new JSONArray();
		JSONArray json2=new JSONArray();
		JSONArray json3=new JSONArray();
		System.out.println("111111");
		//查询全部产品信息
		List<Product> list=pService.findAll();
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		for (Product p : list) {
			JSONObject jo=new JSONObject();
			jo.put("proId", p.getProId());
			jo.put("proName", p.getProName());
			jo.put("proSpe", p.getProSpe());
			jo.put("kinName", p.getKinName());
			jo.put("proBen", p.getProBen());
			jo.put("proWill", p.getProWill());
			jo.put("proSale", p.getProSale());
			jo.put("proBrand", p.getProBrand());
			jo.put("proDate", sdf.format(p.getProDate()));
			jo.put("proPic", p.getProPic());
			jo.put("proNote", p.getProNote());
			jo.put("proUnit", p.getProUnit());
			json1.add(jo);
		}
		//查询全部品牌
		List<Brand> list2=bService.findAll();
		for (Brand b : list2) {
			JSONObject jo=new JSONObject();
			jo.put("braId", b.getBraId());
			jo.put("braName", b.getBraName());
			json2.add(jo);
		}
		//查询全部产品分类
		List<Kind> list3=kService.findAll();
		for (Kind k : list3) {
			JSONObject jo=new JSONObject();
			jo.put("kinId", k.getKinId());
			jo.put("kinName", k.getKinName());
			json3.add(jo);
		}
		json.put("product", json1);
		json.put("brand", json2);
		json.put("kind", json3);
		return json;
	}
	
	/**
	 * 新增产品
	 * @param product
	 * @return
	 */
	@RequestMapping("/addproduct")
	public JSONObject addproduct(Product product) {
		System.out.println("产品名称: "+product.getProName());
		JSONObject json=new JSONObject();
		product.setProId("pro0006");
		String returnString=pService.addProduct(product);
		json.put("ans", returnString);
		return json;
	}
	
	@RequestMapping("/product")
	@ResponseBody
	public String findProduct(String form) {
		JSONObject forms = JSONObject.parseObject(form);
		System.out.println(forms);
		Map map = JSONObject.toJavaObject(forms, Map.class);
		System.out.println(map);
		System.out.println(map.values());
		// map.put("kin_name", )
		return JSON.toJSONString(ps.findPurduct(map));
	}
	
	/**
	 * 
	 * @param product
	 * @return
	 */
	@RequestMapping("/updateproduct")
	public JSONObject updateproduct(Product product) {
		JSONObject json=new JSONObject();
		System.out.println("id来了没???"+product.getProId());
		pService.editProduct(product);
		json.put("ans", "y");
		return json;
	}
}
